package com.bdmc.car.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdmc.car.domain.CarMaintenance;
import com.bdmc.car.domain.CarMaintenanceDetail;
import com.bdmc.car.domain.bo.CarMaintenanceBo;
import com.bdmc.car.domain.vo.CarMaintenanceVo;
import com.bdmc.car.mapper.CarMaintenanceMapper;
import com.bdmc.car.service.ICarMaintenanceDetailService;
import com.bdmc.car.service.ICarMaintenanceService;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 维修信息Service业务层处理
 *
 * @author bdmc
 * @date 2023-01-06
 */
@Service
public class CarMaintenanceServiceImpl extends ServicePlusImpl<CarMaintenanceMapper, CarMaintenance, CarMaintenanceVo> implements ICarMaintenanceService {

    @Autowired
    ICarMaintenanceDetailService carMaintenanceDetailService;


    @Override
    public CarMaintenanceVo queryById(Integer maintenanceId){
        return getVoById(maintenanceId);
    }

    @Override
    public TableDataInfo<CarMaintenanceVo> queryPageList(CarMaintenanceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CarMaintenance> lqw = buildQueryWrapper(bo);
        Page<CarMaintenanceVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<CarMaintenanceVo> mySelectPage(CarMaintenanceBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<CarMaintenanceVo> queryList(CarMaintenanceBo bo) {
        LambdaQueryWrapper<CarMaintenance> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<CarMaintenance> buildQueryWrapper(CarMaintenanceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CarMaintenance> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getReporter()), CarMaintenance::getReporter, bo.getReporter());
        lqw.eq(bo.getDeliveryTime() != null, CarMaintenance::getDeliveryTime, bo.getDeliveryTime());
        lqw.eq(bo.getFinishTime() != null, CarMaintenance::getFinishTime, bo.getFinishTime());
        lqw.eq(bo.getGarageId() != null, CarMaintenance::getGarageId, bo.getGarageId());
        lqw.eq(StringUtils.isNotBlank(bo.getEnterBy()), CarMaintenance::getEnterBy, bo.getEnterBy());
        return lqw;
    }

    @Transactional
    @Override
    public Boolean insertByBo(CarMaintenanceBo bo) {
        CarMaintenance add = BeanUtil.toBean(bo, CarMaintenance.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setMaintenanceId(add.getMaintenanceId());
            for (CarMaintenanceDetail detail : bo.getCarMaintenanceDetailList()) {
                detail.setMaintenanceId(add.getMaintenanceId());
            }
            carMaintenanceDetailService.saveBatch(bo.getCarMaintenanceDetailList());
        }
        return flag;
    }

    @Transactional
    @Override
    public Boolean updateByBo(CarMaintenanceBo bo) {
        CarMaintenance update = BeanUtil.toBean(bo, CarMaintenance.class);
        validEntityBeforeSave(update);
        boolean flag = updateById(update);
        if (flag) {
            for (CarMaintenanceDetail detail : bo.getCarMaintenanceDetailList()) {
                if(detail.getMaintenanceId()==null){
                    detail.setMaintenanceId(update.getMaintenanceId());
                }
            }
            carMaintenanceDetailService.saveOrUpdateBatch(bo.getCarMaintenanceDetailList());
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(CarMaintenance entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
